package AlibabaSMS

import (
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)

var accessKeyId = "LTAI4GEsyxqrMRwKdsuUgyR7"
var accessSecret = "Rju1nU0lNRu3Gyl8ietmQll4RTxtph"

// 发送报名成功短信 只能一个手机号 两个参数iphione何name
func Successful_registration(w http.ResponseWriter, r *http.Request) {
	fmt.Println("调用发送报名成功接口")
	client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", accessKeyId, accessSecret)

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"
	data := r.URL.Query()
	// 获取手机号
	request.PhoneNumbers = data.Get("iphone")
	request.SignName = "泸州市龙马潭区老年大学"
	request.TemplateCode = "SMS_211481183"
	// 获取姓名
	request.TemplateParam = `{"mtname":` +`"`+data.Get("class_name") +`"`+ `}`

	response, err := client.SendSms(request)
	if err != nil {
		fmt.Print(err.Error())
		jsonk := `{"isok":"no","msg":"发送失败"}`
		fmt.Println("返回前台数据为：", jsonk)
		w.Write([]byte(jsonk)) //给客户端返回数据。
		return
	}
	fmt.Printf("response is %#v\n", response)
	jsonk := `{"isok":"ok","msg":"发送完成"}`
	fmt.Println("返回前台数据为：", jsonk)
	w.Write([]byte(jsonk)) //给客户端返回数据。
}

// 发送短信 获取短信验证码
func Verification_code(w http.ResponseWriter, r *http.Request) {
	client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", accessKeyId, accessSecret)

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"
	data := r.URL.Query()
	// 获取手机号
	request.PhoneNumbers = data.Get("iphone")
	request.SignName = "泸州市龙马潭区老年大学"
	request.TemplateCode = "SMS_210340120"
	rand.Seed(time.Now().UnixNano())
	value := 1000+rand.Intn(100000)%8999         //生成4位数随机整数
	request.TemplateParam = `{"code":`+strconv.Itoa(value)+`}`

	response, err := client.SendSms(request)
	if err != nil {
		fmt.Print(err.Error())
		jsonk:=`{"isok":"no","code":}`
		w.Write([]byte(jsonk)) //给客户端返回数据。
		return
	}
	fmt.Printf("response is %#v\n", response)
	jsonk:=`{"isok":"ok","code":`+strconv.Itoa(value)+`}`
	fmt.Println("返回前台数据为：",jsonk)
	w.Write([]byte(jsonk)) //给客户端返回数据。
}